import {
    AddArticleComment,
    ApiRes,
    ArticleComment,
    ArticleDetail,
} from "@/types/data";
import { RootThunkAction } from "@/types/store";
import { http } from "@/utils/http";
// 导入 dayjs 相关包
import dayjs from "dayjs";
import localizedFormat from "dayjs/plugin/localizedFormat";
dayjs.extend(localizedFormat);
// 获得文章数据
export const getArticleById = (id: string): RootThunkAction => {
    return async (dispatch) => {
        const {
            data: { data: detail },
        } = await http.get<ApiRes<ArticleDetail>>(`/articles/${id}`);
        dispatch({
            type: "article/get",
            payload: {
                ...detail,
                // 格式化日期：
                pubdate: dayjs(detail.pubdate).format("LL"),
            },
        });
    };
};
// 处理关注
export const followAuthor = (
    id: string,
    isFollowed: boolean
): RootThunkAction => {
    return async (dispatch) => {
        if (isFollowed) {
            // 取关
            await http.delete(`/user/followings/${id}`);
        } else {
            // 关注
            await http.post("/user/followings", {
                target: id,
            });
        }

        dispatch({
            type: "article/updateInfo",
            payload: {
                name: "is_followed",
                value: !isFollowed,
            },
        });
    };
};
// 收藏 or 取消收藏文章
export const collectArticle = (
    art_id: string,
    isCollected: boolean
): RootThunkAction => {
    return async (dispatch) => {
        // 判断当前是否收藏
        if (isCollected) {
            // 取消收藏
            http.delete(`/article/collections/${art_id}`);
        } else {
            // 收藏
            http.post("/article/collections", {
                target: art_id,
            });
        }

        dispatch({
            type: "article/updateInfo",
            payload: {
                name: "is_collected",
                value: !isCollected,
            },
        });
    };
};
// 点赞 or 取消点赞文章
export const likeArticle = (
    art_id: string,
    attitude: number
): RootThunkAction => {
    return async (dispatch) => {
        // 判断当前是否点赞
        // 如果是 1 表示已点赞
        if (attitude === 1) {
            // 取消点赞
            await http.delete(`/article/likings/${art_id}`);
        } else {
            // 点赞
            await http.post("/article/likings", {
                target: art_id,
            });
        }

        dispatch({
            type: "article/updateInfo",
            payload: {
                name: "attitude",
                value: attitude === 1 ? 0 : 1,
            },
        });
    };
};
// 第一次加载评论
export const getArticleComments = (
    type: string,
    id: string
): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.get<ApiRes<ArticleComment>>("comments", {
            params: {
                type,
                source: id,
            },
        });
        dispatch({
            type: "article/getArticleCommentsFirst",
            payload: res.data.data,
        });
    };
};
// 继续加载评论
export const getMoreArticleComments = (
    type: string,
    id: string,
    offset: string | null
): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.get<ApiRes<ArticleComment>>("/comments", {
            params: {
                type,
                source: id,
                offset,
            },
        });
        dispatch({
            type: "article/getArticleComments",
            payload: res.data.data,
        });
    };
};
// 对文章发表评论
export const addArticleComment = (
    target: string,
    content: string
): RootThunkAction => {
    return async (dispatch) => {
        const res = await http.post<ApiRes<AddArticleComment>>("/comments", {
            target,
            content,
        });

        dispatch({
            type: "article/addComment",
            payload: res.data.data.new_obj,
        });
    };
};
// 点赞 or 取消点赞评论
export const likeComment = (
    art_id: string,
    is_liking: boolean
): RootThunkAction => {
    return async (dispatch) => {
        if (is_liking) {
            // 取消点赞
            await http.delete(`/comment/likings/${art_id}`);
        } else {
            // 点赞
            await http.post("/comment/likings", {
                target: art_id,
            });
        }

        dispatch({
            type: "comment/updateInfo",
            payload: {
                // name 表示要修改状态中的哪个属性
                name: "is_liking",
                // value 表示要修改成哪个值
                value: !is_liking,
                // 评论 id
                target: art_id,
                // 数量
                like_count: is_liking ? -1 : 1,
            },
        });
    };
};
// 更新某个评论项的回复数量
export const updateCommentCount = (
    commentId: string,
    total: number
): RootThunkAction => {
    return async (dispatch) => {
        dispatch({
            type: "article/updateCommentCount",
            payload: {
                commentId,
                total,
            },
        });
    };
};
